//
//  XRcordEditViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XRcordEditViewController.h"
#import <Realm/Realm.h>

#import "XTextField.h"

@interface XRcordEditViewController ()<UITextViewDelegate>


@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// 标题
@property(nonatomic,strong)XTextField* titleTextField;

/// 占位文本
@property(nonatomic,strong)UILabel* placeLb;

/// 内容
@property (nonatomic,weak)UITextView* contentTextView;

@end

@implementation XRcordEditViewController



- (void)loadView{
    [super loadView];
    self.view.backgroundColor = UIColor.whiteColor;
    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-IPHONE_NAVIGATION_BAR_HEIGHT)];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    [self.view addSubview:scrollView];
    
    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.padding = UIEdgeInsetsZero;
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setupViews];
    
    if(self.operation == EditOperation_Edit){
        [self updateViews];
    }
}

- (void)setupNavBar{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //自定义星期显示
    dateFormatter.weekdaySymbols = @[@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六"];
    //自定义日期显示
    dateFormatter.dateFormat = @"yyyy/MM/dd EEEE";
    NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
    
    UIButton* dateBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 128, 32)];
    dateBtn.titleLabel.font = FONT(14);
    [dateBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [dateBtn setTitle:dateStr forState:UIControlStateNormal];
    dateBtn.layer.cornerRadius = 5;
    dateBtn.layer.borderWidth = Size_1PX;
    dateBtn.layer.borderColor = COLOR_WHITE.CGColor;
    self.navigationItem.titleView = dateBtn;
    
    
    UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"x_edit_save"] style:(UIBarButtonItemStylePlain) target:self action:@selector(confimClicked:)];
    self.navigationItem.rightBarButtonItem = saveItem;
    
}

- (void)setupViews{
    
    
    NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"标题" attributes: @{NSForegroundColorAttributeName:COLOR_GRAY_QUITE}];
    
    XTextField* titleTextField = [[XTextField alloc] init];
    titleTextField.myHorzMargin = 16;
    titleTextField.myTop = 20;
    titleTextField.myHeight = PX(44);
    titleTextField.textField.attributedPlaceholder = placeholder;
    titleTextField.bottomBorderline = [[MyBorderline alloc] initWithColor:COLOR_GRAY_BASE thick:1 headIndent:8 tailIndent:8 offset:2.0f];
    [self.contentLayout addSubview:titleTextField];
    _titleTextField = titleTextField;
    
    
    MyRelativeLayout* subLayout = [MyRelativeLayout new];
    subLayout.backgroundColor = UIColor.whiteColor;
    subLayout.myHorzMargin = 16;
    subLayout.myTop = 16;
    subLayout.myHeight = MyLayoutSize.wrap;
    subLayout.layer.cornerRadius = PX(5);
    subLayout.layer.masksToBounds = YES;
    [self.contentLayout addSubview:subLayout];

    UITextView* contentTextView = [UITextView new];
    contentTextView.backgroundColor = UIColor.whiteColor;
    contentTextView.font = FONT_FIT(14);
    contentTextView.textColor = COLOR_BLACK_DARK;
    contentTextView.delegate = self;
    contentTextView.scrollEnabled = NO;
    contentTextView.returnKeyType = UIReturnKeyDone;
    contentTextView.showsHorizontalScrollIndicator = NO;
    contentTextView.showsVerticalScrollIndicator = NO;
    contentTextView.tintColor = COLOR_THEME_A;
    contentTextView.myMargin = 0;
    contentTextView.myHeight = PX(100);
    [subLayout addSubview:contentTextView];
    _contentTextView = contentTextView;

    UILabel* placeLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_QUITE text:@"写下今天的生活..."];
    placeLb.topPos.equalTo(contentTextView.topPos).offset(8);
    placeLb.leftPos.equalTo(contentTextView.leftPos).offset(8);
    [subLayout addSubview:placeLb];
    _placeLb = placeLb;
}

- (void)updateViews{
    
    self.titleTextField.textField.text = self.record.title;

    self.contentTextView.text = self.record.content;
    [self.contentTextView sizeToFit];
    
    self.placeLb.visibility = self.record.content.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
}

#pragma mark  --- action

///确定
- (void)confimClicked:(UIButton*)sender{

    
    NSString* title = self.titleTextField.textField.text;
    title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSString* content = self.contentTextView.text;
    content = [content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if(!title.length){
        title = self.titleTextField.textField.placeholder;
    }
    
    if(!content.length){
        [Toast toast:@"请输入记录内容"];
        return;
    }

    if(self.operation == EditOperation_Add){


        XDynamicModel* model = [XDynamicModel new];
        model.id = [NSDate date].timeIntervalSince1970;
        model.type = XDynamicTypeRecord;
        model.title = title;
        model.content = content;
        model.date = [NSDate date];
        [[RLMRealm defaultRealm] transactionWithBlock:^{
            [[RLMRealm defaultRealm] addObject:model];
        }];

        if(self.editBlock){
            self.editBlock(model, self.operation);
        }

    }else if (self.operation == EditOperation_Edit){

        [[RLMRealm defaultRealm] transactionWithBlock:^{
            self.record.title = title;
            self.record.content = content;
            [[RLMRealm defaultRealm] addOrUpdateObject:self.record];
        }];
        if(self.editBlock){
            self.editBlock(self.record, self.operation);
        }
    }
    [self closeViewControllerAnimated:YES];
}


#pragma mark  -- UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    self.placeLb.visibility = textView.text.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
}
@end
